package by.averianov.discounta.webapp.page.shop;

import java.util.Iterator;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.Page;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.averianov.discounta.datamodel.Product;
import by.averianov.discounta.datamodel.Product_;
import by.averianov.discounta.services.SaleService;
import by.averianov.discounta.webapp.page.BaseLayout;
import by.averianov.discounta.webapp.page.create.CreateCard;
import by.averianov.discounta.webapp.page.shop.shopcart.ShopCart;
import by.averianov.discounta.webapp.page.table.Row;

public class InShop extends BaseLayout {

	private static final Logger LOGGER = LoggerFactory.getLogger(InShop.class);
	
	protected Long selectProductId;
	protected Long productShopId;
	
	public InShop(Long selectId, Long productShop) {
		super(null);
		super.currentMenu = "shop";
		this.selectProductId = selectId;
		this.productShopId = productShop;
	}
	
	@Inject
	private SaleService saleService;

	private class ProductDataProvider extends SortableDataProvider<Product, SingularAttribute<Product, ?>> {

		public ProductDataProvider() {
			super();
			setSort(Product_.nameProduct, SortOrder.ASCENDING);
		}
	
		@Override
		public Iterator<? extends Product> iterator(long first, long count) {
	
			SingularAttribute<Product, ?> sortParam = getSort().getProperty();
			SortOrder propertySortOrder = getSortState().getPropertySortOrder(sortParam);
			boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);
			return saleService.getAllProductNotNull(sortParam, ascending, (int) first, (int) count).iterator();
		}
	
		@Override
		public long size() {
			return saleService.getCountProductNotNull();
		}
	
		@Override
		public IModel<Product> model(Product product) {
			return new CompoundPropertyModel<Product>(product);
		}
	}
    
    @Override
	protected void onInitialize() {
		super.onInitialize();
		
		Link linkAddProduct = new SecuredLinkForSpokesman("linkAdd") {
			
			@Override
			public void onClick() {
				setResponsePage(new CreateCard(null, new PageCreator() {

					@Override
					public Page createPage() {
						return new InShop(0L, 0L);
					}
				}));
				LOGGER.debug("Add Row");
			}
		};
		linkAddProduct.add(new Label("labelAdd", getButtonAddProduct()));
		if (currentMenu == "shop") {
			linkAddProduct.setVisible(false);
		}
		add(linkAddProduct);
		
		Link linkShopCart = new SecuredLinkForAll("linkShopCart") {
			
			@Override
			public void onClick() {
				setResponsePage(new ShopCart(saleService.getProductById(selectProductId), new PageCreator() {

					@Override
					public Page createPage() {
						return new InShop(0L, 0L);
					}
				}));
				LOGGER.debug("Add Row");
			}
		};
		linkShopCart.add(new Label("labelShopCart", getButtonToShopCart()));
		add(linkShopCart);
		
    	setOutputMarkupId(true);
    	ProductDataProvider productDataProvider = new ProductDataProvider();

		final WebMarkupContainer tableBody = new WebMarkupContainer("wrapper-body") {
			@Override
			protected void onBeforeRender() {
				super.onBeforeRender();
			}
		};
	
		tableBody.setOutputMarkupId(true);
		add(tableBody);
	
		DataView<Product> dataView = new DataView<Product>("list", productDataProvider, 15) {

			@Override
			protected void populateItem(Item<Product> item) {
				final Product product = item.getModelObject();
                Boolean isSelected = false;
                Long sPId = selectProductId;
                Long checkPId = product.getId();
                Long pSId = productShopId;
                Long checkPSId = product.getShop().getId();
                if(sPId == checkPId & pSId == checkPSId) {
					isSelected = true;
				}
				
				item.add(new Row("row", product, isSelected, currentMenu));
				item.setVisible(true);                				
			}
			
		};
	
		tableBody.add(dataView);	
		add(new PagingNavigator("paging", dataView));	
		add(new OrderByBorder<SingularAttribute<Product, ?>>("sortById", Product_.id, productDataProvider));
		add(new OrderByBorder<SingularAttribute<Product, ?>>("sortByName", Product_.nameProduct, productDataProvider));
		add(new OrderByBorder<SingularAttribute<Product, ?>>("sortByPrice", Product_.priceProduct, productDataProvider));
		add(new OrderByBorder<SingularAttribute<Product, ?>>("sortByDiscStatus", Product_.discountStatus, productDataProvider));
		add(new OrderByBorder<SingularAttribute<Product, ?>>("sortByShop", Product_.shop, productDataProvider));
		add(new OrderByBorder<SingularAttribute<Product, ?>>("sortByCheckout", Product_.checkoutCounter, productDataProvider));
		add(new OrderByBorder<SingularAttribute<Product, ?>>("sortByCount", Product_.countProduct, productDataProvider));
		
		LOGGER.debug("onInitialize table");		
	}
    

	protected IModel<String> getButtonAdd() {
		return new ResourceModel("p.button.add");
	}
}
